﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class DisController : Controller
    {
        //
        // GET: /Discipline/
        public JsonResult GetPagingGrid(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from dt in context.DSKyLuat
                            where dt.IDCanBo == id
                            select dt).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.HinhThucKyLuat,
                                 customer.NgayKL,
                                 customer.NgayXoaKL,
                                 customer.GhiChu
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index(int id = 0)
        {
            List<KyLuat> dsqhgd = KyLuatModels.GetDSKyLuatTheoCanBo(id);
            ViewBag.DSKyLuat = dsqhgd;
            CanBo cb = CanBoModels.GetChiTietCanBo(id);
            ChucVu cv = PosModels.GetChiTietDSChucVu((int)cb.ChucVuHienTai);
            PhongBan pb = PhongBanModels.GetChiTietPhongBan(cb.IDPhongBan);
            ViewBag.ChucVu = cv;
            ViewBag.PhongBan = pb;
            ViewBag.ChiTietCanBo = cb;
            return View(dsqhgd);
        }
        public ActionResult Add(int id = 0)
        {

            ViewBag.Id = id;
            return View();
        }
        [HttpPost]
        public ActionResult Add(FormCollection form, int id)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
            KyLuat themKyLuat = new KyLuat();
            themKyLuat.HinhThucKyLuat = form["HinhThucKyLuat"];
            themKyLuat.NgayKL = DateTime.Parse(form["NgayKL"],ci.DateTimeFormat);
            themKyLuat.NgayXoaKL = DateTime.Parse(form["NgayXoaKL"],ci.DateTimeFormat);
            themKyLuat.GhiChu = form["GhiChu"];

            themKyLuat.IDCanBo = id;
            //themQHGD.IDCanBo = id;

            KyLuatModels.Add(themKyLuat);
            //return RedirectToAction("Index");
            return RedirectToAction("Index", "Dis", new { id = themKyLuat.IDCanBo });
        }
        public ActionResult Update(int id)
        {
            KyLuat listQHGD = KyLuatModels.GetChiTietDSKyLuat(id);
            ViewBag.DSKyLuat = listQHGD;
            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection form, int id)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
            KyLuat s = KyLuatModels.GetChiTietDSKyLuat(id);
            
            s.HinhThucKyLuat = form["HinhThucKyLuat"];
            s.NgayKL = DateTime.Parse(form["NgayKL"],ci.DateTimeFormat);
            s.NgayXoaKL = DateTime.Parse(form["NgayXoaKL"],ci.DateTimeFormat);
            s.GhiChu = form["GhiChu"];
                        
            KyLuatModels.Update(s);
            return RedirectToAction("Index", "Dis", new { id = s.IDCanBo });
        }
        public ActionResult Delete(int id)
        {
            KyLuat s = KyLuatModels.GetChiTietDSKyLuat(id);
            KyLuatModels.Delete(id);

            return RedirectToAction("Index", "Dis", new { id = s.IDCanBo });
        }

    }
}
